﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.UI.Skins.Skin
Imports WillStrohl.Modules.Quiz.QuizController
Imports WillStrohl.Modules.Quiz.QuizInfoMembers

Namespace WillStrohl.Modules.Quiz

    Partial Public MustInherit Class EditAnswer
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private p_AnswerId As Integer = Null.NullInteger
        Private p_QuestionId As Integer = Null.NullInteger
        Private p_Question As QuestionInfo = Nothing

#End Region

#Region " Properties "

        Private ReadOnly Property AnswerId() As Integer
            Get
                If p_AnswerId = Null.NullInteger Then
                    Dim obj As Object = Request.Params(QUERYSTRING_ANSWER_ID_KEY)
                    If Not obj Is Nothing Then
                        p_AnswerId = Integer.Parse(obj.ToString(), Globalization.NumberStyles.Integer)
                    End If
                End If

                Return p_AnswerId
            End Get
        End Property

        Private ReadOnly Property QuestionId As Integer
            Get
                If p_QuestionId = Null.NullInteger Then
                    Dim obj As Object = Request.Params(QUERYSTRING_QUESTION_ID_KEY)
                    If Not obj Is Nothing Then
                        p_QuestionId = Integer.Parse(obj.ToString(), Globalization.NumberStyles.Integer)
                    End If
                End If

                Return p_QuestionId
            End Get
        End Property

        Protected ReadOnly Property Question As QuestionInfo
            Get
                If p_Question Is Nothing Then
                    Dim ctlQuiz As New QuizController
                    p_Question = ctlQuiz.GetQuestion(QuestionId)
                End If

                Return p_Question
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub cmdCancel_Click(sender As Object, e As System.EventArgs) Handles cmdCancel.Click
            SendToAnswersView()
        End Sub

        Private Sub cmdDelete_Click(sender As Object, e As System.EventArgs) Handles cmdDelete.Click
            DeleteAnswer()
            SendToAnswersView()
        End Sub

        Private Sub cmdUpdate_Click(sender As Object, e As System.EventArgs) Handles cmdUpdate.Click
            If Page.IsValid Then
                SaveAnswer()
                SendToAnswersView()
            End If
        End Sub

#End Region

#Region " Private Helper Functions "

        Private Sub BindData()

            Me.LocalizeModule()

            cmdDelete.Visible = (AnswerId > 0)

            If AnswerId > 0 Then
                Dim ctlQuiz As New QuizController
                Dim oAnswer As AnswerInfo = ctlQuiz.GetAnswer(AnswerId)
                txtAnswer.Text = oAnswer.Answer
                chkActive.Checked = oAnswer.IsActive
                chkCorrect.Checked = oAnswer.IsCorrect
            Else
                chkActive.Checked = True
                chkCorrect.Checked = False
            End If

        End Sub

        Private Sub LocalizeModule()

            rfvAnswer.ErrorMessage = GetLocalizedString("rfvAnswer.ErrorMessage")
            cmdCancel.Text = GetLocalizedString("cmdCancel.Text")
            cmdDelete.Text = GetLocalizedString("cmdDelete.Text")
            cmdUpdate.Text = GetLocalizedString("cmdUpdate.Text")

        End Sub

        Private Sub SaveAnswer()

            Dim sec As New PortalSecurity
            Dim ctlQuiz As New QuizController
            Dim oAnswer As New AnswerInfo

            oAnswer.Answer = sec.InputFilter(txtAnswer.Text, PortalSecurity.FilterFlag.NoScripting)
            oAnswer.IsCorrect = chkCorrect.Checked
            oAnswer.IsActive = chkActive.Checked
            oAnswer.LastModifiedBy = UserId
            oAnswer.QuestionId = QuestionId

            If AnswerId > 0 Then
                oAnswer.AnswerId = AnswerId
                ctlQuiz.UpdateAnswer(oAnswer)
            Else
                ctlQuiz.AddAnswer(oAnswer)
            End If

        End Sub

        Private Sub DeleteAnswer()

            Dim ctlQuiz As New QuizController
            ctlQuiz.DeleteAnswer(AnswerId)

        End Sub

        Private Sub SendToAnswersView()
            Response.Redirect(EditUrl(String.Empty, String.Empty, "Answers", String.Format("{0}={1}", QUERYSTRING_QUESTION_ID_KEY, QuestionId)))
        End Sub

        Private Sub HandleException(ByVal exc As Exception)
            LogException(exc)
            If Me.UserInfo.IsSuperUser Or Me.UserInfo.UserID = PortalSettings.AdministratorId Then
                AddModuleMessage(Me, _
                    String.Concat(exc.Message, "<br />", exc.StackTrace), _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            Else
                AddModuleMessage(Me, exc.Message, _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End If
        End Sub

#End Region
        
    End Class

End Namespace